function isNotEmptyTable(_trs)
{
	return 	_trs && _trs.length>0;
}
  
LookupControl=function(_trs,_callback)
{
  this.cur=-1;
	this.component;
	this.colorOnOut="";
	this.colorOnOver="#666";
	this.trs;
	this.callback;
  this.startTrNo=0;
  _theControl=this;
  
  this.setComponent=function(comp)
	{
		this.component=comp;
	};
	
	this.returnValue=function()
	{
		if(this.cur>=0)
  	{
  		var tr=jQuery(this.trs[this.cur]);
  		return this.callback(tr);
  	}
  	else
  	{
  		return null;
  	}
	};
	
	this.setValue=function(_value)
	{
		if(this.component)
    {
    	this.component.resetValue(_value);
    }
	};
	
	this.returnAndSetValue=function(tr)
	{
		var _value=this.callback(jQuery(tr));
		this.setValue(_value);
	};

	this.gotoTrByNo=function(trNo)
	{
		if(this.cur>=0)
    {
    	jQuery(this.trs[this.cur]).css("background-color",this.colorOnOut);
  	}
  	this.cur=trNo;
  	jQuery(this.trs[this.cur]).css("background-color",this.colorOnOver);
	};
	
	this.gotoTrOnOver=function(tr)
	{
		if(this.cur>=0)
    {
    	jQuery(this.trs[this.cur]).css("background-color",this.colorOnOut);
  	}
  	this.cur=jQuery(tr).attr("rowIndex")-this.startTrNo;
  	jQuery(tr).css("background-color",this.colorOnOver);
	};
	
	this.gotoPrevious=function()
	{
	  if(!isNotEmptyTable(this.trs))
	  {
	  	return ;
	  }
	  if(this.cur==-1)
  	{
  		this.gotoTrByNo(0);
  	}
  	else
  	{
  		if(this.cur==0)
  		{
  			this.gotoTrByNo(this.trs.length-1);
  		}
  		else
  		{
  			this.gotoTrByNo(this.cur-1);
  		}
  	}
	};
	
	this.gotoNext=function()
	{
		if(!isNotEmptyTable(this.trs))
	  {
	  	return ;
	  }
		if(this.cur==-1 || this.cur==this.trs.length-1)
  	{
  		this.gotoTrByNo(0);
  	}
		else
		{
			this.gotoTrByNo(this.cur+1);
  	}
	};
	
  this.init=function(_trs,_callback)
  {
  	this.trs=_trs;
  	this.callback=_callback;
  	
  	if(isNotEmptyTable(_trs))
  	{
	  	this.startTrNo=jQuery(_trs[0]).attr("rowIndex");

	  	for(i=0;i<_trs.length;i++)
	  	{
	  		var _theTR=jQuery(_trs[i]);
	  		
	  		_theTR.click(function()
	  		{
	  			_theControl.returnAndSetValue(this);
	  		});
	  		_theTR.mouseover(function()
	  		{
	  			_theControl.gotoTrOnOver(this);
	  		});
	  	}
		}
  };
  
  this.init(_trs,_callback);
};

var _LOOKUPCONTROL=new LookupControl(null,function(tr){});   
